﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace XMLDAL
{

    public class NewsDAL : INewsDAL
    {
        public Boolean InsertNews(News news)
        {
            try
            {
                NewsDataClassesDataContext newsContext = new NewsDataClassesDataContext();
                newsContext.News.InsertOnSubmit(news);
                newsContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteNews(int newsId)
        {
            try
            {
                NewsDataClassesDataContext newsContext = new NewsDataClassesDataContext();
                News news = (from u in newsContext.News
                             where u.NewsId.Equals(newsId)
                             select u).First();
                newsContext.News.DeleteOnSubmit(news);
                newsContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateNews(int newsId, News news)
        {

            if (newsId != news.NewsId)
            {
                return false;
            }
            NewsDataClassesDataContext newsContext = new NewsDataClassesDataContext();
            News query = (from u in newsContext.News
                          where u.NewsId.Equals(newsId)
                          select u).First();
            query.Password = news.Password;


            newsContext.SubmitChanges();
            return true;

        }

        public News SelectNewsById(int newsId)
        {
            NewsDataClassesDataContext newsContext = new NewsDataClassesDataContext();
            News news = (from u in newsContext.News
                         where u.NewsId.Equals(newsId)
                         select u).First();
            return news;
        }


        public List<News> SelectAllNews()
        {
            NewsDataClassesDataContext newsContext = new NewsDataClassesDataContext();
            var query = (from u in newsContext.News
                         select u);
            List<News> newsList = new List<News>();

            foreach (var news in query)
            {
                newsList.Add(news);
            }
            return newsList;
        }

    }
}
